package com.xiaoai.io.nio1BufferTest;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *
 *
 *
 * @Author
 * @Date 2021-09-07 00:43
 */
public class BufferTest {

    @Test
    public void write() {
        try {
            FileOutputStream fos = new FileOutputStream("data01.txt");
            // 得到输出流对应通过channel
            FileChannel channel = fos.getChannel();
            // 分配缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            buffer.put("hello,黑马java程序员！".getBytes());
            buffer.flip();
            channel.write(buffer);
            channel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void read() {
        try {
            FileInputStream fis = new FileInputStream("data01.txt");
            // 获取channel
            FileChannel channel = fis.getChannel();
            // 定义缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            // 读取数据到缓冲区
            channel.read(buffer);
            buffer.flip();
            String rs = new String(buffer.array(), 0, buffer.remaining());
            System.out.println(rs);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void copy() {
        try {
            File srcFile = new File(System.getProperty("user.dir") + "\\res\\server\\1f913498-65f5-4683-98fe-1a9025a2bd87.png");
            File destFile = new File(System.getProperty("user.dir") + "\\res\\server\\new_1f913498-65f5-4683-98fe-1a9025a2bd87.png");

            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);

            FileChannel isChannel = fis.getChannel();
            FileChannel osChannel = fos.getChannel();

            // 分配缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (true) {
                // 读取数据前应先清空缓冲区
                buffer.clear();

                // 开始读一次数据
                int flag = isChannel.read(buffer);
                if (flag == -1) {
                    break;
                }
                // 读取数据后把缓冲区切换为可读模式
                buffer.flip();
                // 数据写出
                osChannel.write(buffer);
            }
            isChannel.close();
            osChannel.close();
            System.out.println("复制完成！");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //分散和聚集
    @Test
    public void test() throws IOException {
        RandomAccessFile raf1 = new RandomAccessFile("data01.txt", "rw");
        //1. 获取通道
        FileChannel channel1 = raf1.getChannel();

        //2. 分配指定大小的缓冲区
        ByteBuffer buf1 = ByteBuffer.allocate(10);
        ByteBuffer buf2 = ByteBuffer.allocate(1024);

        //3. 分散读取
        ByteBuffer[] bufs = {buf1, buf2};
        channel1.read(bufs);

        for (ByteBuffer byteBuffer : bufs) {
            byteBuffer.flip();
        }

        System.out.println(new String(bufs[0].array(), 0, bufs[0].limit()));
        System.out.println("-----------------");
        System.out.println(new String(bufs[1].array(), 0, bufs[1].limit()));

        //4. 聚集写入
        RandomAccessFile raf2 = new RandomAccessFile("data02.txt", "rw");
        FileChannel channel2 = raf2.getChannel();
        channel2.write(bufs);
    }


    @Test
    public void transfer() throws Exception {
        // 1、字节输入管道
        FileInputStream is = new FileInputStream("data01.txt");
        FileChannel isChannel = is.getChannel();
        // 2、字节输出流管道
        FileOutputStream fos = new FileOutputStream("data03.txt");
        FileChannel osChannel = fos.getChannel();
        // 3、复制
        osChannel.transferFrom(isChannel,isChannel.position(),isChannel.size());
        isChannel.close();
        osChannel.close();
    }

    @Test
    public void transferTo() throws Exception {
        // 1、字节输入管道
        FileInputStream is = new FileInputStream("data01.txt");
        FileChannel isChannel = is.getChannel();
        // 2、字节输出流管道
        FileOutputStream fos = new FileOutputStream("data04.txt");
        FileChannel osChannel = fos.getChannel();
        // 3、复制
        isChannel.transferTo(isChannel.position() , isChannel.size() , osChannel);
        isChannel.close();
        osChannel.close();
    }



}